package com.decent.ejfadmin.user.dao.provider;

import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.bean.UserFeedback;
import com.decent.ejfadmin.utils.SQLWrapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * 会员意见反馈
 *
 * @author weicx
 * @date 2018/08/20
 */
public class UserFeedbackDaoProvider {
    /**
     * 查询会员反馈计数
     *
     * @param param 参数
     * @return sql
     */
    public String queryFeedbackCount(HashMap<String, Object> param) {
        StringBuffer sql = new StringBuffer(100);
        sql.append("select count(1)  ");
        generateFeedbackQuerySql(sql, param);
        return sql.toString();
    }

    /**
     * 分页查询会员反馈
     *
     * @param param 参数
     * @return sql
     */
    public String queryFeedbackPage(HashMap<String, Object> param) {
        StringBuffer sql = new StringBuffer(100);
        sql.append("select f.id,u.account,i.name,f.userId,f.userPhone,f.feedback ,f.status,f.feedBackTime,ifnull(f.admin,'') admin," +
                "f.dealTime,ifnull(f.remark,'') remark ");
        generateFeedbackQuerySql(sql, param);
        sql.append(" order by id desc ");
        Page page = (Page) param.get("page");
        return SQLWrapUtils.wrapPage(sql, page.getBeginIndex(), page.getSize());
    }


    /**
     * 查询会员反馈组织参数
     *
     * @param sql   sql
     * @param param 参数
     */
    private void generateFeedbackQuerySql(StringBuffer sql, HashMap<String, Object> param) {
        UserFeedback userFeedback = (UserFeedback) param.get("userFeedback");
        sql.append(" FROM\n" +
                "\tyuhuang_sup_feedback f\n" +
                "LEFT JOIN  \n" +
                "\tyuhuang_sup_user u on f.userId = u.user_id\n" +
                "LEFT JOIN \n" +
                "\tyuhuang_sup_info i on u.user_id = i.user_id\n" +
                " where feedBackTime >= #{userFeedback.startTime} and feedBackTime <= #{userFeedback.endTime} ");
        //会员条件
        if (StringUtils.isNotBlank(userFeedback.getAccount())) {
            sql.append(" AND  (instr(u.account, #{userFeedback.account})>0 or instr(i.name, #{userFeedback.account})>0) ");
        }
        //联系电话
        if (StringUtils.isNotBlank(userFeedback.getUserPhone())) {
            sql.append(" and f.userPhone = #{userFeedback.userPhone} ");
        }
        //状态
        if (userFeedback.getStatus() != null) {
            sql.append(" and f.status = #{userFeedback.status} ");
        }
        //处理管理员
        if (StringUtils.isNotBlank(userFeedback.getAdmin())) {
            sql.append(" and instr(f.admin,#{userFeedback.admin}) > 0 ");
        }
        //反馈
        if (StringUtils.isNotBlank(userFeedback.getFeedback())) {
            sql.append(" and instr(f.feedback,#{userFeedback.feedback}) > 0 ");
        }
        //处理备注
        if (StringUtils.isNotBlank(userFeedback.getRemark())) {
            sql.append(" and instr(f.remark,#{userFeedback.remark}) > 0 ");
        }
    }

}